package cn.stylefeng.guns.cloud.workflow.modular.controller;

import cn.stylefeng.guns.cloud.libs.scanner.annotation.PostResource;
import cn.stylefeng.guns.cloud.libs.scanner.stereotype.ApiResource;
import cn.stylefeng.guns.cloud.model.exp.RequestEmptyException;
import cn.stylefeng.guns.cloud.model.page.PageResult;
import cn.stylefeng.guns.cloud.model.web.response.ResponseData;
import cn.stylefeng.guns.cloud.workflow.modular.model.params.DeployProcessParam;
import cn.stylefeng.guns.cloud.workflow.modular.model.result.DeployProcessResult;
import cn.stylefeng.guns.cloud.workflow.modular.service.DeployProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


/**
 * 流程部署控制器
 *
 * @Author xuyuxiang
 * @Date 2019/11/14 19:40
 **/
@RestController
@ApiResource(name = "流程部署管理接口", path = "/deployProcess")
@Api(tags = "流程部署管理接口")
public class DeployProcessController {

    @Autowired
    private DeployProcessService deployProcessService;

    /**
     * 获取流程部署列表(分页)
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:41
     **/
    @PostResource(name = "获取流程部署列表(分页)", path = "/list")
    @ApiOperation(value = "获取流程部署列表(分页)", response = DeployProcessResult.class)
    public ResponseData list(@RequestBody DeployProcessParam deployProcessParam) {
        PageResult pageResult = deployProcessService.list(deployProcessParam, true);
        return ResponseData.success(pageResult);
    }

    /**
     * 获取流程部署版本列表(分页)
     *
     * @return
     * @Author xuyuxiang
     * @Date 2019/11/14 19:41
     */
    @PostResource(name = "获取流程部署版本列表(分页)", path = "/versionList")
    @ApiOperation(value = "获取流程部署版本列表(分页)", response = DeployProcessResult.class)
    public ResponseData versionList(@RequestBody DeployProcessParam deployProcessParam) {
        PageResult pageResult = deployProcessService.list(deployProcessParam, false);
        return ResponseData.success(pageResult);
    }

    /**
     * 删除已部署的流程
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:43
     **/
    @PostResource(name = "删除已部署的流程", path = "/delete")
    @ApiOperation(value = "删除已部署的流程")
    public ResponseData delete(@RequestParam("deploymentId") String deploymentId) {
        deployProcessService.delete(deploymentId);
        return ResponseData.success();
    }

    /**
     * 查看已部署流程图,前端直接请求url
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:43
     **/
    @PostResource(name = "查看已部署流程图", path = "/viewPic")
    @ApiOperation(value = "查看已部署流程图")
    public void viewPic(@RequestBody DeployProcessParam deployProcessParam,
                        HttpServletResponse response) {
        deployProcessService.viewPic(deployProcessParam, response);
    }

    /**
     * 导入流程资源
     *
     * @Author xuyuxiang
     * @Date 2019/11/19 11:46
     **/
    @PostResource(name = "导入流程资源", path = "/importProcess")
    @ApiOperation(value = "导入流程资源")
    public ResponseData importProcess(@RequestPart("file") MultipartFile file) {
        if (null != file && !file.isEmpty()) {
            deployProcessService.importProcess(file);
        } else {
            throw new RequestEmptyException("流程资源文件为空!");
        }
        return ResponseData.success();
    }

    /**
     * 导出流程
     * exportFileType 为json或bpmn(xml)
     *
     * @Author xuyuxiang
     * @Date 2019/11/8 14:35
     **/
    @PostResource(name = "导出已部署流程图", path = "/export")
    @ApiOperation(value = "导出已部署流程图")
    public void export(@RequestBody DeployProcessParam deployProcessParam,
                       HttpServletResponse response) {
        deployProcessService.export(deployProcessParam, response);
    }

    /**
     * 打包下载（xml和png）
     *
     * @Author xuyuxiang
     * @Date 2019/11/19 11:41
     **/
    @PostResource(name = "打包下载已部署流程图", path = "/downloadZip")
    @ApiOperation(value = "打包下载已部署流程图")
    public void downloadZip(@RequestParam String deploymentId,
                            HttpServletResponse response) throws IOException {
        deployProcessService.downloadZip(deploymentId, response);
    }

    /**
     * 映射模型
     *
     * @Author xuyuxiang
     * @Date 2019/11/19 11:51
     **/
    @PostResource(name = "映射模型", path = "/mapping")
    @ApiOperation(value = "映射模型")
    public ResponseData mapping(@RequestParam("processDefinitionId") String processDefinitionId) throws UnsupportedEncodingException, XMLStreamException {
        deployProcessService.mapping(processDefinitionId);
        return ResponseData.success();
    }


    /**
     * 挂起
     *
     * @Author xuyuxiang
     * @Date 2019/11/17 16:31
     **/
    @PostResource(name = "挂起部署的流程", path = "/suspend")
    @ApiOperation(value = "挂起部署的流程")
    public ResponseData suspend(@RequestParam("processDefinitionId") String processDefinitionId) {
        deployProcessService.suspend(processDefinitionId);
        return ResponseData.success();
    }

    /**
     * 激活
     *
     * @Author xuyuxiang
     * @Date 2019/11/17 16:31
     **/
    @PostResource(name = "激活部署的流程", path = "/active")
    @ApiOperation(value = "激活部署的流程")
    public ResponseData active(@RequestParam("processDefinitionId") String processDefinitionId) {
        deployProcessService.active(processDefinitionId);
        return ResponseData.success();
    }

}


